package com.shop.front.controller.system;

import com.alibaba.fastjson.JSON;
import com.shop.domain.system.Version;
import com.shop.front.util.IpUtil;
import com.shop.front.vo.system.SystemCitySimpleVo;
import com.shop.domain.pay.PayConfig;
import com.shop.front.service.system.SystemService;
import com.shop.web.cache.RedisCache;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(description = "系统相关", position = -1)
@RestController
@RequestMapping(value = "/system", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class SystemController {

    @Autowired
    private SystemService systemService;
    @Autowired
    private RedisCache redisCache;


    @ApiOperation(value = "城市列表")
    @ApiResponses(@ApiResponse(code = 10000, response = SystemCitySimpleVo.class, message = "content"))
    @RequestMapping("/findIsShowCity")
    public BaseResponse findIsShowCity() {
        return systemService.findCitySimple();
    }


    @ApiOperation(value = "支付参数配置")
    @ApiResponses(@ApiResponse(code = 10000, response = PayConfig.class, message = "content"))
    @RequestMapping("/findPayConf")
    public BaseResponse findPayConf() {
        List<PayConfig> list = null;

        // 支付配置缓存key
        int cacheTime = 60;  // 秒
        String cacheKey = "payConf_";
        // 缓存是否存在
        String cacheVal = redisCache.getString(cacheKey);
        if(StringUtils.isNotBlank(cacheVal)) {
            list = JSON.parseObject(cacheVal, List.class);
            return new BaseResponse(list);
        }

        // 读取数据库配置
        list = systemService.findPayConf();

        // 缓存返回值
        redisCache.set(cacheKey, JSON.toJSONString(list), cacheTime);

        return new BaseResponse(list);
    }


    @ApiOperation(value = "获得用户IP地址")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @RequestMapping("/getUserIp")
    public String getUserIp(HttpServletRequest request) {
        String ip = IpUtil.getIp(request);
        String ip6 = IpUtil.getIPv6(request);
        log.info("IP4地址为：{},ip6地址为{}",ip,ip6);
        return ip;
    }


    /**
     * 获得系统最新版本信息
     */
    @ApiOperation(value = "获得系统最新版本信息")
    @ApiResponses(@ApiResponse(code = 10000, response = Version.class, message = "content"))
    @RequestMapping("/version")
    public BaseResponse version() {

        return systemService.getLastVersion();
    }


    @ApiOperation(value = "微信分享appid")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @RequestMapping("/wxAppId")
    public BaseResponse wxAppId() {

        return systemService.shareWxAppid();
    }



}
